﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.Main_Base.DigitalMap
{
    public partial class Main_Base_DigitalMap_Info_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public Main_Base_DigitalMap_Info_ManaForm()
        {
            InitializeComponent();
        }

        private void Main_Base_DigitalMap_Info_ManaForm_Load(object sender, EventArgs e)
        {
            bind();
        }
        public void bind()
        {
            string sql = @"select f_id,f_name,f_size,(select f_name from TS_S_T_P_Stuff where f_id=TS_S_B_Digital_Map_Main.f_workerid) as f_stuname,f_time from TS_S_B_Digital_Map_Main";
            gridControl.DataSource = TS_Win_Class.GetFilledDataTable(sql);
        }
        public static string id;
        private void gridControl_Click(object sender, EventArgs e)
        {
            id = gridView1.GetRowCellDisplayText(gridView1.FocusedRowHandle, "f_id").ToString();
            DataTable dtb = TS_Win_Class.GetFilledDataTable("select f_name,f_size from TS_S_B_Digital_Map_Main where f_id=" + id);
            tbname.Text = dtb.Rows[0]["f_name"].ToString();
            tbsize1.Text = dtb.Rows[0]["f_size"].ToString().Split(',')[0];
            tbsize2.Text = dtb.Rows[0]["f_size"].ToString().Split(',')[1];
            flowLayoutPanel.Controls.Clear();
            try
            {
                CheckBox cb = new CheckBox();
                string sql = @"select f_name,f_id,(select count(*) from TS_S_B_R_Digital_Map_Main_Station where f_del=0 and f_dmid=" + id + " and f_swid=TS_S_B_Station_WorkPlace.f_id) as count from TS_S_B_Station_WorkPlace where f_del=0 and f_deep=0";
                DataTable dt = TS_Win_Class.GetFilledDataTable(sql);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    cb = new CheckBox();
                    cb.Width = 200;
                    cb.Name = "cb_" + dt.Rows[i]["f_id"].ToString();
                    cb.Tag = dt.Rows[i]["f_id"].ToString();
                    cb.Text = dt.Rows[i]["f_name"].ToString();
                    if (dt.Rows[i]["count"].ToString() != "0")
                    {
                        cb.Checked = true;
                    }
                    cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
                    flowLayoutPanel.Controls.Add(cb);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        void cb_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)sender).Checked)
            {
                string sql = "insert into TS_S_B_R_Digital_Map_Main_Station (f_dmid,f_swid,f_workerid) values(" + id + "," + ((CheckBox)sender).Tag.ToString() + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                MessageBox.Show(sql);
                TS_Win_Class.MySqlCom(sql);
            }
            else
            {
                string sql = "update TS_S_B_R_Digital_Map_Main_Station set f_del=1 where f_dmid=" + id + " and f_swid=" + ((CheckBox)sender).Tag.ToString();
                TS_Win_Class.MySqlCom(sql);
            }
        }

        private void btsave_Click(object sender, EventArgs e)
        {
            if (tbname.Text.Trim() != "" && tbsize1.Text.Trim() != "" && tbsize2.Text.Trim() != "")
            {
                string sql = "insert into TS_S_B_Digital_Map_Main (f_name,f_size,f_workerid) values('" + tbname.Text.Trim() + "','" + tbsize1.Text.Trim() + "," + tbsize2.Text.Trim() + "'," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                TS_Win_Class.MySqlCom(sql);
                bind();
            }
            else
            {
                MessageBox.Show("请确认名称或尺寸填写完整!");
            }
        }

        private void btcler_Click(object sender, EventArgs e)
        {
            tbname.Text = "";
            tbsize1.Text = "";
            tbsize2.Text = "";
        }

        private void btup_Click(object sender, EventArgs e)
        {
            if (tbname.Text.Trim() != "" && tbsize1.Text.Trim() != "" && tbsize2.Text.Trim() != "")
            {
                // string sql = "insert into TS_S_B_Digital_Map_Main (f_name,f_size,f_workerid) values('" + tbname.Text.Trim() + "','" + tbsize1.Text.Trim() + "," + tbsize2.Text.Trim() + "'," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                string sql = "update TS_S_B_Digital_Map_Main set f_name='" + tbname.Text + "',f_size='" + tbsize1.Text.Trim() + "," + tbsize2.Text.Trim() + "' where f_id=" + id;
                TS_Win_Class.MySqlCom(sql);
                bind();
            }
            else
            {
                MessageBox.Show("请确认名称或尺寸填写完整!");
            }
        }

        private void tbsize1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
        }

        private void Main_Base_DigitalMap_Info_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }
    }
}